package com.ffcs.ffxtc.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.ffcs.aop.Loggable;
import com.ffcs.ffxtc.dao.DvCategoryDao;
import com.ffcs.ffxtc.domain.dto.CategoryDTO;
import com.ffcs.ffxtc.domain.dto.CategoryExcelDTO;
import com.ffcs.ffxtc.domain.dto.SumDTO;
import com.ffcs.ffxtc.domain.entity.Demo;
import com.ffcs.ffxtc.domain.entity.DvCategory;
import com.ffcs.ffxtc.domain.vo.CategoryVO;
import com.ffcs.ffxtc.mapper.DvCategoryMapper;
import com.ffcs.utils.BeanUtils;
import com.ffcs.utils.ExcelUtils;
import com.ffcs.utils.base.R;
import com.ffcs.utils.base.Request;
import com.github.yulichang.wrapper.MPJLambdaWrapper;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.validation.Valid;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.util.List;

/**
 * <p>
 * 组件类目表 前端控制器
 * </p>
 *
 * @author 张正兴
 * @since 2024-04-13
 */
@RestController
@RequestMapping("/dvCategory")
@Tag(name = "组件类目表", description = "")
@RequiredArgsConstructor
public class DvCategoryController {

    private final DvCategoryDao dvCategoryDao;

    private final DvCategoryMapper dvCategoryMapper;

    @PostMapping("/json")
    @Operation(summary = "json转换")
    public R<CategoryVO> findJson(@Valid @RequestBody CategoryVO categoryVo) {
        String json = categoryVo.getJson();
        return R.ok(categoryVo);
    }


    @PostMapping("/leftJoin")
    @Operation(summary = "关联查询")
    public R<Page<CategoryDTO>> findLeftJoin(@RequestBody CategoryVO categoryVo) {
        Page<CategoryDTO> categoryDtoPage = dvCategoryMapper.selectJoinPage(new Page<>(1, 10), CategoryDTO.class,
                new MPJLambdaWrapper<DvCategory>()
                        // 显示当前表全部字段
                        .selectAll(CategoryDTO.class)
                        // 显示指定字段
                        .select(CategoryDTO::getId, CategoryDTO::getName)
                        // 设置别名
                        .selectAs(CategoryDTO::getName, CategoryDTO::getTest)

                        // 聚合List实体类(DvCategory.class映射的实体类)（CategoryDto::getCategor映射实体对象）
                        .selectCollection(DvCategory.class, CategoryDTO::getCategoryList)
                        // 聚合Obj实体类
                        .selectAssociation(DvCategory.class, CategoryDTO::getCategory)

                        // 关联的表
                        .leftJoin(DvCategory.class, DvCategory::getId, DvCategory::getId)

                        // 条件
                        .eq(DvCategory::getId, 83)

                        // 去除重复的数据
                        .distinct()

        );

        // 聚合查询
        List<SumDTO> sumDtos = dvCategoryMapper.selectJoinList(SumDTO.class,
                new MPJLambdaWrapper<DvCategory>()
                        .selectSum(DvCategory::getId, SumDTO::getSum)
                        .groupBy(DvCategory::getName)
        );
        return R.ok(categoryDtoPage);
    }

    @GetMapping("/mapperJoin")
    public R<IPage<List<CategoryDTO>>> mapperJoin(CategoryVO categoryVo) {
        return R.ok(dvCategoryMapper.getCategoryList(new Page<>(categoryVo.getPageNum(),
                categoryVo.getPageSize()), categoryVo));
    }

    @GetMapping("/exportExcel")
    @Operation(summary = "导出excel")
    public void exportExcel() {
        ExcelUtils.exportExcel(dvCategoryDao.list(),
                "我的excel", CategoryExcelDTO.class);
    }

    @PostMapping("/importExcel")
    @Operation(summary = "导入excel")
    public void importExcel(@RequestParam("file") MultipartFile file) {
        ExcelUtils.importExcel(file, CategoryExcelDTO.class);
    }

    /**
     * 分页查询组件类目表
     *
     * @param request
     * @return
     */
    @GetMapping
    @Operation(summary = "分页查询组件类目表")
    @Loggable("类目录查询")
    public R<Page<Demo>> findPageDvCategory(Request request) {
        QueryWrapper<DvCategory> query = new QueryWrapper<>();
        Page<DvCategory> pageObj = dvCategoryDao.page(
                new Page<>(request.getPageNum(), request.getPageSize()), query);
        return R.ok(BeanUtils.toBeanPage(pageObj, Demo.class));
    }

}